package cn.stylefeng.guns.modular.fsjs.service.impl;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.stylefeng.guns.core.shiro.ShiroKit;
import cn.stylefeng.guns.core.util.CacheUtil;
import cn.stylefeng.guns.core.util.ServerResponse;
import cn.stylefeng.guns.core.util.Token;
import cn.stylefeng.guns.modular.fsjs.service.IAccountService;
import cn.stylefeng.guns.modular.ry600.service.IClientService;
import cn.stylefeng.guns.modular.system.dao.AccountMapper;
import cn.stylefeng.guns.modular.system.dao.ClientMapper;
import cn.stylefeng.guns.modular.system.model.Account;
import cn.stylefeng.guns.modular.system.model.BackMsg;
import cn.stylefeng.guns.modular.system.model.Client;
import cn.stylefeng.guns.modular.system.model.ClientCheck;
import cn.stylefeng.guns.modular.system.model.Register;
import cn.stylefeng.roses.core.util.ToolUtil;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author stylefeng
 * @since 2018-12-06
 */
@Service
public class IAccountServiceImpl  implements IAccountService {
	@Autowired
	private AccountMapper accountMapper;

	@Override
	public Object register(Account register) {
		if((int)this.ifExists(register)>0){
			return ServerResponse.createByErrorMessage("该机构名称已经被注册或用户名重复！");
		}else if(this.doMsg(register)>0){
			return ServerResponse.createBySuccessMessage("注册成功");
		}else{
			return ServerResponse.createByErrorMessage("注册失败，请联系管理员");
		}
	}

	@Override
	public Object getBackPW(Account backMsg) {
		BackMsg tmp=(BackMsg)backMsg;
		if("".equals(tmp.getPassword().trim())||!tmp.getPassword().equals(tmp.getPassword_confirmation()) ){
			return  ServerResponse.createByErrorMessage("密码不一致或密码为空");
		}
		List<String> salt=(List<String>)this.ifExists(backMsg);
		if(salt.size()>0){
			tmp.setSalt(salt.get(0));
		}
		if(salt.size()==0){
			return ServerResponse.createByErrorMessage("账号信息不存在或联系方式错误");
		}else if(this.doMsg(tmp)>0){
			return ServerResponse.createBySuccessMessage("修改密码成功");
		}else{
			
			return ServerResponse.createByErrorMessage("修改密码失败，请联系管理员");
		}
	}

	private Object ifExists(Account autoMsg) {
		if(autoMsg instanceof Register){
			return accountMapper.ifExistsRegister((Register)autoMsg);
		}else if(autoMsg instanceof BackMsg){
			return accountMapper.ifExistsBackMsg((BackMsg)autoMsg);
		}
		return 0;
	}
	
	private int doMsg(Account autoMsg){
		if(autoMsg instanceof Register){
			Register register=(Register)autoMsg;
			String salt=ToolUtil.getRandomString(5);
			register.setSalt(salt);
			register.setRoleid(register.getUserType());
			if("2".equals(register.getUserType())){
				register.setUserType("供水企业");
			}else if("3".equals(register.getUserType())){
				register.setUserType("非供水企业");
			}
			register.setPassword(ShiroKit.md5(register.getPassword(), salt));
			return accountMapper.insertRegister(register);
		}else if(autoMsg instanceof BackMsg){
			BackMsg tmp=(BackMsg)autoMsg;
			tmp.setPassword(ShiroKit.md5(tmp.getPassword(),tmp.getSalt()));
			return accountMapper.updateBackMsg(tmp);
		}
		return 0;
	}
	
}
